package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ProvinceDao;
import com.admission.model.Province;
import com.admission.util.DeleteFile;

@Repository
@Transactional
public class ProvinceManager {
	private ProvinceDao provinceDao;
	private ProvinceScoreManager provinceScoreManager;
	
	/**
	 * 通过id查找位置对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public Province getById(String id) {
		return provinceDao.findUniqueBy("id", id);
	}
	
	public void save(Province entity,String sub){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if (sub != null) {
			File file = new File(serverrealPath + File.separator
					+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件	
			entity.setUrl(sub);
		}
		provinceDao.save(entity);
	}
	
	/**
	 * 通过filter分页查询
	 * 
	 * @param filters
	 * @return
	 */
	public Page<Province> getPage(Page<Province> page,List<PropertyFilter> filters) {
		return provinceDao.findPage(page,filters);
	}

	
	/**
	 * 批量删除
	 * 逗号隔开id集合
	 * @param ids
	 */
	public void deleteByIds(String ids){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		for(String id:ids.split(",")){
			Province entity=this.getById(id.trim());
			File file = new File(serverrealPath + File.separator
						+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件
			provinceScoreManager.deleteByIds(id.trim());
			provinceDao.delete(id.trim());
		}
	}

	public ProvinceDao getProvinceDao() {
		return provinceDao;
	}
	@Autowired
	public void setProvinceDao(ProvinceDao provinceDao) {
		this.provinceDao = provinceDao;
	}

	public ProvinceScoreManager getProvinceScoreManager() {
		return provinceScoreManager;
	}
	@Autowired
	public void setProvinceScoreManager(ProvinceScoreManager provinceScoreManager) {
		this.provinceScoreManager = provinceScoreManager;
	}
	
	
}
